﻿using UnityEngine;
using System.Collections;

namespace UI
{

    public class UICopyLevelItem :MonoBehaviour
    {
        public UICopy copy;
        public string id;

        public int seq;

        public GameObject unSelection;
        public GameObject selection;

        public UISprite levelIcon;

        public GameObject starModule;
        public GameObject[] stars;

        public UILabel levelName;
        public UILabel levelNumber;

        public string bigIcon;

        public GameObject newFlag;


        #region font
        string levelNumberTxt;
        #endregion

        public GameObject lockFlag;

        void Awake()
        {
            levelNumberTxt = TableTextBlo.GetText( "copy_level_num" );
        }

        public void Init(UILevelData levelData,UICopy copy)
        {
            this.levelNumber.text = string.Format( levelNumberTxt, levelData.level_seq );
            this.levelName.text = levelData.level_name;
            this.levelIcon.spriteName = levelData.level_small_icon;
            this.id = levelData.level_id;
            this.copy = copy;
            this.bigIcon = levelData.level_big_icon;
            this.seq = levelData.level_seq;

            if(this.id == copy.currentSelectionLevelId)
            {
                unSelection.SetActive( false );
                selection.SetActive( true );

            }
            else
            {
                unSelection.SetActive( true );
                selection.SetActive( false );
            }

            SetStar( levelData.level_inst_star );

            lockFlag.SetActive(!UICopyData.CurrentLevelOpened(id));

            starModule.SetActive(UICopyData.CurrentLevelOpened(id));

            if(UICopyData.CurrentLevelOpened(this.id))
            {
                newFlag.SetActive(UICopyData.IsNew(this.id));
            }
        }

        public void OnClick()
        {
            if( !UICopyData.CurrentLevelOpened( copy.currentSelectionChapterId, id ) )
            {
                //BoxManager.CreatePopupTextBox( "未开启当前关卡！" );
                return;
            }

            copy.OnRefreshLevel( this.id );
            copy.currLevel.text = this.levelNumber.text;
            copy.currLevelName.text = this.levelName.text;
            copy.SetMainCopyTexture( bigIcon );
            copy.GetComponent<UICopyEvent>().OnClickLevel();
        }

        void SetStar(int lightCount)
        {
            if( lightCount > 5 ) lightCount = 5;
            if( lightCount < 0 ) lightCount = 0;

            foreach (var star in stars)
            {
                star.SetActive( false );
            }

            for( int i=0; i<lightCount; i++ )
            {
                stars[i].SetActive( true );
            }
        }
    }
}